﻿using Core.Entities;
using Core.IPepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Repositories
{
    public class UserRepository : EntityRepository<User>, IUserRepository
    {
        private readonly BMSContext _db;
        public UserRepository(BMSContext db) : base(db)
        {
            _db = db;
        }
        /// <summary>
        /// 检查用户是存在
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>存在返回用户实体，否则返回NULL</returns>
        public User CheckUser(string userName, string password)
        {
            return _db.Set<User>().FirstOrDefault(it => it.UserName == userName && it.Password == password);
        }

        public List<User> GetAllUser()
        {
            return _db.Set<User>().Where(it => it.IsDeleted == 0).ToList();
        }

        public User GetUserInfo(Guid userID)
        {
            return _db.Set<User>().FirstOrDefault(it => it.ID == userID);
        }

        public User GetWithRoles(Guid id) 
        {
            var user = _db.Set<User>().FirstOrDefault(t => t.ID == id);
            if (user != null)
            {
                user.UserRoles = _db.Set<UserRole>().Where(t => t.UserID == user.ID).ToList();
            }
            return user;
        }
    }
}
